const {getPublicKeySync} = require("../utils/rsaControl");
const User = require("../models/User");
const expressJwt = require('express-jwt')
module.exports = () => {
  return expressJwt({
    secret: getPublicKeySync(), //解密秘钥
    algorithms: ["RS256"], //6.0.0以上版本必须设置解密算法
    isRevoked: async (req, payload, next) => {
      let {_id} = payload
      req._id = _id
      req.isPass = true
      try {
        let result = await User.findById(_id)
        if (!result) {
          req.isPass = false
        }
        next()
      } catch (err) {
        next(err)
      }
    }
  }).unless({
    path: [
      {url: '/admin/login'},
      {url: '/admin/register'},
      {url: '/keys', methods: ['GET']},
      {url: '/indexbanners', methods: ['GET']},
      {url: '/newslist', methods: ['GET']},
      {url: /^\/news\/.*/, methods: ['GET']},
      {url: '/application', methods: ['POST']},
    ]
  })
}
